package com.neusoft.yiyang.user.controller;

import com.neusoft.yiyang.common.entity.user.User;
import com.neusoft.yiyang.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "系统用户管理接口", description = "负责系统用户的创建、查询、更新及删除，关联角色信息维护用户权限")
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @Operation(summary = "获取所有用户", description = "查询系统中所有未删除的用户基本信息（不含角色详情）")
    @GetMapping
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }

    @Operation(summary = "获取用户及关联角色", description = "查询所有未删除用户的信息及对应的角色详情（含角色名称、权限等）")
    @GetMapping("/with-role")
    public List<Map<String, Object>> getAllUsersWithRole() {
        return userService.getAllUsersWithRole();
    }

    @Operation(summary = "根据ID查询用户", description = "获取指定ID的用户详细信息（不含角色信息）")
    @GetMapping("/{id}")
    public User getUserById(
            @Parameter(description = "用户ID", required = true)
            @PathVariable("id") Integer id) {
        return userService.getUserById(id);
    }

    @Operation(summary = "根据用户名查询用户", description = "通过用户名获取对应的用户信息（用于登录验证等场景）")
    @GetMapping("/username/{username}")
    public User getUserByUsername(
            @Parameter(description = "用户名（登录账号）", required = true)
            @PathVariable("username") String username) {
        return userService.getUserByUsername(username);
    }

    @Operation(summary = "创建用户", description = "新增系统用户（含账号、密码、角色关联等信息），返回创建结果及用户ID")
    @PostMapping("/create")
    public Map<String, Object> createUser(
            @Parameter(description = "用户信息（含用户名、密码、角色ID等）", required = true)
            @RequestBody User user) {
        return userService.createUser(user);
    }

    @Operation(summary = "更新用户", description = "修改指定ID的用户信息（如密码、状态、角色关联等），返回更新结果")
    @PutMapping("/update/{id}")
    public Map<String, Object> updateUser(
            @Parameter(description = "用户ID", required = true) @PathVariable("id") Integer id,
            @Parameter(description = "更新后的用户信息", required = true) @RequestBody User user) {
        user.setUserId(id);
        return userService.updateUser(user);
    }

    @Operation(summary = "删除用户", description = "逻辑删除指定ID的用户（标记为已删除，不实际移除数据）")
    @DeleteMapping("/{id}")
    public void deleteUser(
            @Parameter(description = "用户ID", required = true) @PathVariable("id") Integer id) {
        userService.deleteUser(id);
    }

    @Operation(summary = "修改用户密码", description = "根据用户ID修改密码，需验证旧密码并提供新密码")
    @PutMapping("/update-password/{id}")
    public Map<String, Object> updatePassword(
            @Parameter(description = "用户ID", required = true) @PathVariable("id") Integer id,
            @Parameter(description = "密码修改信息（旧密码、新密码）", required = true)
            @RequestBody Map<String, String> passwordInfo) {

        String oldPassword = passwordInfo.get("oldPassword");
        String newPassword = passwordInfo.get("newPassword");
        return userService.updatePassword(id, oldPassword, newPassword);
    }
}